package com.sky.controller.admin;

/**
 * @Author 褚金龙
 * @Date 2024/9/22 22:13
 */

import com.sky.dto.setmeal.SetMealDTO;
import com.sky.entity.SetMeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Setmeal.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 添加套餐
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin")
@Api(tags = "套餐表")
@Slf4j
public class SetMealController {
     @Autowired
     private SetmealService setmealService;
    @ApiModelProperty("添加套餐")
    @PostMapping("/setmeal/add")
    @CacheEvict(cacheNames ="setmealCache" ,key = "#setMeal.categoryId")  //删除对应的缓存数据
    public Result addSetMeal(@RequestBody SetMeal setMeal){
       // log.info("{}",setMeal);
        setmealService.addSetMeal(setMeal);
        return Result.success();
    }

    @ApiModelProperty("分页查询套餐")
    @GetMapping ("/setmeal/page")
    public Result<PageResult> querySetMeal( SetMealDTO setMealDTO){
         log.info("{}",setMealDTO);
        PageResult setMeals = setmealService.query(setMealDTO);
       // System.out.println(setMeals);
        return Result.success(setMeals);
    }

    //根据id回显套餐
    @ApiModelProperty("根据id查询套餐")
    @GetMapping ("/setmeal/query/{id}")
    public Result<SetMeal> getSetMeal(@PathVariable Long id){
       SetMeal setMeal=  setmealService.getByIdSetMeal(id);

        return Result.success(setMeal);
    }


    @ApiModelProperty("根据id修改套餐")
    @PutMapping ("/setmeal/update")
    @CacheEvict(cacheNames ="setmealCache" ,key = "#setMeal.categoryId")  //删除对应的缓存数据
    public Result<SetMeal> updateSetMeal(@RequestBody SetMeal setMeal){
        log.info("setMeal{}",setMeal);
          setmealService.updateSetMeal(setMeal);

        return Result.success();
    }


    @ApiModelProperty("根据id删除套餐")
    @DeleteMapping ("/setmeal")
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true) //删除所有缓存
    public Result<SetMeal> deleteSetMeal(@RequestParam List<Long> ids){
        // log.info("setMeal{}",setMeal);
        setmealService.deleteSetMeal(ids);

        return Result.success();
    }


    @ApiOperation("根据分类id修改状态")
    @PostMapping("/setmeal/status/{status}")
    @CacheEvict(cacheNames = "setmealCache" ,key = "#id") //删除所有缓存
    public Result updataStatus(@PathVariable Integer status ,@RequestBody Long id ){
        log.info("status{}id{}",status,id);
         setmealService.updateStatus(status,id);
        return Result.success();
    }


}
